package thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class Demo38 {
    public static void main(String[] args) throws InterruptedException {
        //将任务拆分成10个子任务 同时执行
        //for循环的个数与分成多少个子任务个数一致
        CountDownLatch countDownLatch=new CountDownLatch(10);
        ExecutorService executors= Executors.newFixedThreadPool(4 );
        for (int i = 0; i < 5; i++) {
            int id=i;
            executors.submit(()->{
                System.out.println("开始执行："+id);
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("结束执行："+id);
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        System.out.println("子任务执行完毕！");
        executors.shutdown();
    }
}
